home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / ipdump.c < prev    next >
C/C++ Source or Header  |  1991-07-17  |  2KB  |  96 lines

  1. /* @(#) $Header: ipdump.c,v 1.6 91/07/16 17:55:22 deyke Exp $ */
  2.  
  3. /* IP header tracing routines
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "iface.h"
  11. #include "ip.h"
  12. #include "trace.h"
  13. #include "netuser.h"
  14.  
  15. void
  16. ip_dump(fp,bpp,check)
  17. FILE *fp;
  18. struct mbuf **bpp;
  19. int check;
  20. {
  21.     struct ip ip;
  22.     int16 ip_len;
  23.     int16 length;
  24.     int16 csum;
  25.  
  26.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  27.         return;
  28.  
  29.     fprintf(fp,"IP:");
  30.     /* Sneak peek at IP header and find length */
  31.     ip_len = ((*bpp)->data[0] & 0xf) << 2;
  32.     if(ip_len < IPLEN){
  33.         fprintf(fp," bad header\n");
  34.         return;
  35.     }
  36.     if(check)
  37.         csum = cksum(NULLHEADER,*bpp,ip_len);
  38.     else
  39.         csum = 0;
  40.  
  41.     ntohip(&ip,bpp);        /* Can't fail, we've already checked ihl */
  42.  
  43.     /* Trim data segment if necessary. */
  44.     length = ip.length - ip_len;    /* Length of data portion */
  45.     trim_mbuf(bpp,length);
  46.     fprintf(fp," len %u",ip.length);
  47.     fprintf(fp," %s",inet_ntoa(ip.source));
  48.     fprintf(fp,"->%s ihl %u ttl %u",
  49.         inet_ntoa(ip.dest),ip_len,uchar(ip.ttl));
  50.     if(ip.tos != 0)
  51.         fprintf(fp," tos %u",uchar(ip.tos));
  52.     if(ip.offset != 0 || ip.flags.mf)
  53.         fprintf(fp," id %u offs %u",ip.id,ip.offset);
  54.     if(ip.flags.df)
  55.         fprintf(fp," DF");
  56.     if(ip.flags.mf){
  57.         fprintf(fp," MF");
  58.         check = 0;      /* Bypass host-level checksum verify */
  59.     }
  60.     if(ip.flags.congest){
  61.         fprintf(fp," CE");
  62.     }
  63.     if(csum != 0)
  64.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  65.  
  66.     if(ip.offset != 0){
  67.         putc('\n',fp);
  68.         return;
  69.     }
  70.     switch(uchar(ip.protocol)){
  71.     case IP_PTCL:
  72.         fprintf(fp," prot IP\n");
  73.         ip_dump(fp,bpp,check);
  74.         break;
  75.     case TCP_PTCL:
  76.         fprintf(fp," prot TCP\n");
  77.         tcp_dump(fp,bpp,ip.source,ip.dest,check);
  78.         break;
  79.     case UDP_PTCL:
  80.         fprintf(fp," prot UDP\n");
  81.         udp_dump(fp,bpp,ip.source,ip.dest,check);
  82.         break;
  83.     case ICMP_PTCL:
  84.         fprintf(fp," prot ICMP\n");
  85.         icmp_dump(fp,bpp,ip.source,ip.dest,check);
  86.         break;
  87.     case AX25_PTCL:
  88.         fprintf(fp," prot AX25\n");
  89.         ax25_dump(fp,bpp,check);
  90.         break;
  91.     default:
  92.         fprintf(fp," prot %u\n",uchar(ip.protocol));
  93.         break;
  94.     }
  95. }
  96.